package servlet;

import entity.Movie;
import test.selectMovies;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author IlseTian
 * @ 2020-07-01- 20:02
 **/
@WebServlet(name = "MoviesServlet", value = "/MoviesServlet")
public class MoviesServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");//防止中文乱码
        //解决中文乱码
        response.setContentType("text/html;charset=utf-8");
        //请求解决乱码
        request.setCharacterEncoding("utf-8");
        //响应解决乱码
        response.setCharacterEncoding("utf-8");

        //获取用户选择的匹配类型：
        String type = request.getParameter("type");
        System.out.println("---type--" + type);
        //获取用户输入的电影关键字：
        String movieName = request.getParameter("movieName");

        //根据用户选择的匹配类型和电影关键字匹配电影并返回给结果页面展示：
        System.out.println("---movieName--" + movieName);
        selectMovies sm = new selectMovies();
        List<Movie> list = sm.selectOption(type, movieName);
        if (list.isEmpty()) {
            PrintWriter out = response.getWriter();
            out.println("<script language='javascript'>alert('没有数据！');window.location.href='index.jsp'</script>");
        } else {
            //跳转到结果页面：
            request.setAttribute("list", list);
            request.getRequestDispatcher("/searchResult.jsp").forward(request, response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
